package cn.gok.homework;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

class Book{
    private int bid;
    private String name;
    private float price;

    public Book(int bid, String name, float price) {
        this.bid = bid;
        this.name = name;
        this.price = price;
    }

    public int getBid() {
        return bid;
    }

    public void setBid(int bid) {
        this.bid = bid;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public float getPrice() {
        return price;
    }

    public void setPrice(float price) {
        this.price = price;
    }
    public void sell(){
        System.out.println(this.name+"被卖了");
    }
    private void sell(float price,String person){
        System.out.println(this.name+"卖给了"+person+",卖了"+price+"元");
    }
}
public class HomeWork {
    public static void main(String[] args) throws NoSuchMethodException, IllegalAccessException, InstantiationException, InvocationTargetException {
        test1();
    }

    private static void test1() throws NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException {
        //获取Book类信息，并获取全属性构造函数对象constructor
        Class<Book> c = Book.class;
//        Constructor<?>[] constructors = c.getConstructors();
//        System.out.println("全属性构造函数对象:"+constructors);

        Constructor<Book> constructor = c.getConstructor(int.class, String.class, float.class);
        System.out.println("全属性构造函数对象:"+constructor);
        //利用该构造对象，创建1个书的实例。
        Book book = constructor.newInstance(1, "牛马记", 100f);
        System.out.println(book);
//        通过反射，分别获取两个sell方法对应的method对象，
//        并破坏封装性，分别调用该方法。
        Book b = new Book(1, "牛马记", 100f);
        Method m1 = c.getDeclaredMethod("sell");
        Method m2 = c.getDeclaredMethod("sell", float.class, String.class);
        m1.setAccessible(true);
        m2.setAccessible(true);
        m1.invoke(b);
        m2.invoke(b,100f,"giao哥");
    }
}
